﻿using B.MESUI.Login.BasicData_UI.BasicData;
using B.MESUI.Login.Properties;
using DevExpress.XtraEditors;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Runtime.CompilerServices.RuntimeHelpers;

namespace B.MESUI.Login.BasicData_UI.Customer
{
    public partial class AddCustomerForm : DevExpress.XtraEditors.XtraForm
    {
        public delegate void AddCustomer_load();
        public event AddCustomer_load  customerEvent;
        public TextEdit txtCode, txtName, txtAbbreviation, txtPhone, txtAddress, txtCreditCode, txtNode;
        public SimpleButton btnSave, btnCancel;

        public AddCustomerForm()
        {
            InitializeComponent();
            
            this.Text = "新增客户";
            this.Width = 1000;
            this.Height = 1000;
            this.StartPosition = FormStartPosition.CenterScreen;

            // 先清空旧控件，重新构建
            this.Controls.Clear();

            // 顶部间隔，使标题下移
            var topSpacer = new Panel { Dock = DockStyle.Top, Height = 40 };
            this.Controls.Add(topSpacer);

            // 顶部标题
            var title = new Label()
            {
                Text = "新增客户信息",
                Font = new Font("微软雅黑", 20, FontStyle.Bold),
                Dock = DockStyle.Top,
                Height = 80,
                TextAlign = ContentAlignment.MiddleCenter,
            };
            this.Controls.Add(title);

            // 主容器：用于让表单整体居中
            var mainPanel = new Panel { Dock = DockStyle.Fill };
            this.Controls.Add(mainPanel);

            // 表单TableLayoutPanel
            var layout = new TableLayoutPanel
            {
                ColumnCount = 2,
                RowCount = 8, // 7字段 + 1按钮行
                AutoSize = true,
                AutoSizeMode = AutoSizeMode.GrowAndShrink,
                Padding = new Padding(0, 20, 0, 10)
            };
            layout.ColumnStyles.Clear();
            layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 220)); // 标签列加宽，避免文字截断
            layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 400)); // 输入列
            for (int i = 0; i < 7; i++)
                layout.RowStyles.Add(new RowStyle(SizeType.Absolute, 70));
            layout.RowStyles.Add(new RowStyle(SizeType.Absolute, 80)); // 按钮行

            // 字段创建简化方法
            Font lblFont = new Font("微软雅黑", 12);
            Font txtFont = new Font("微软雅黑", 12);
            int r = 0;
            void AddField(string labelText, out TextEdit txt)
            {
                var lbl = new Label { Text = labelText, Font = lblFont, TextAlign = ContentAlignment.MiddleRight, Dock = DockStyle.Fill };
                layout.Controls.Add(lbl, 0, r);
                txt = new TextEdit { Font = txtFont, Dock = DockStyle.Fill };
                layout.Controls.Add(txt, 1, r);
                r++;
                txtCode.Enabled = false;
            }
            
            AddField("客户编号：", out txtCode);
            AddField("客户名称：", out txtName);
            AddField("简称：", out txtAbbreviation);
            AddField("联系电话：", out txtPhone);
            AddField("地址：", out txtAddress);
            AddField("信用代码：", out txtCreditCode);
            AddField("备注：", out txtNode);

            // 按钮区
            var btnPanel = new FlowLayoutPanel { FlowDirection = FlowDirection.LeftToRight, Dock = DockStyle.Fill, AutoSize = true, Anchor = AnchorStyles.Right };
            btnSave = new SimpleButton { Text = "保存", Width = 140, Height = 55, Font = lblFont };
            btnCancel = new SimpleButton { Text = "取消", Width = 140, Height = 55, Font = lblFont };
            btnPanel.Controls.Add(btnSave);
            btnPanel.Controls.Add(btnCancel);
            layout.Controls.Add(btnPanel, 1, r);

            // 将layout放入居中Panel
            var centerPanel = new Panel { AutoSize = true }; // 内层包裹layout
            centerPanel.Controls.Add(layout);
            centerPanel.Location = new Point((mainPanel.Width - layout.Width) / 2, (mainPanel.Height - layout.Height) / 2);
            centerPanel.Anchor = AnchorStyles.None;
            mainPanel.Controls.Add(centerPanel);

            // 事件
            btnCancel.Click += (s, e) => this.Close();
            //点击保存按钮
            btnSave.Click += async (s, e) =>
            { 
                //获取用户输入
                string customerCode = txtCode.Text.Trim();
                
                string customerName = txtName.Text.Trim();
                string customerAbbreviation = txtAbbreviation.Text.Trim();
                string customerPhone = txtPhone.Text.Trim();
                string customerAddress = txtAddress.Text.Trim();
                string customerCreditCode = txtCreditCode.Text.Trim();
                string customerNode = txtNode.Text.Trim();

                AddCustomer addCustomer = new AddCustomer()
                {
                    customeR_CODE=customerCode,
                    customeR_NAME = customerName,
                    customeR_ABBREVIATION= customerAbbreviation,
                    customeR_PHONE= customerPhone,
                    customeR_ADDRESS= customerAddress,
                    crediT_CODE= customerCreditCode,
                    customeR_NODE= customerNode
                };
               var data=JsonConvert.SerializeObject(addCustomer);
                string url = $"http://localhost:5198/api/Customer/AddCustomer";
                //调用添加客户接口
               string result = await BaseMethod.MethodsURL("post",url,data);
                if (result!= "")
                {
                    XtraMessageBox.Show("保存成功！");

                    this.Close(); // 或刷新父窗口数据
                    customerEvent.Invoke();
                }



            };
        }
    }
}